package it.unibo.lmc.pjdbc.driver;

import it.unibo.lmc.pjdbc.database.PrologDatabase;
import it.unibo.lmc.pjdbc.database.command.PResultSet;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class PrologStatement implements Statement {

	private PrologConnection conn = null;
	private PrologDatabase currentDatabase = null;
	
	
	private StringReader currentQuery = null;
	
	private Logger log;
	private ResultSet lastResultSet;

	public PrologStatement(PrologConnection connection,PrologDatabase database) {
		this.conn = connection;
		this.currentDatabase = database;
		log = Logger.getLogger(PrologStatement.class);
	}

	public void addBatch(String sql) throws SQLException {
		
		
	}

	public void cancel() throws SQLException {
		
		
	}

	public void clearBatch() throws SQLException {
		
		
	}

	public void clearWarnings() throws SQLException {
		
		
	}

	public void close() throws SQLException {
		
		
	}

	public boolean execute(String sql) throws SQLException {
		log.debug("query: "+sql);
		PResultSet res = this.currentDatabase.executeQuery(sql);
		this.lastResultSet = new PrologResultSet(sql,res,this.currentDatabase, null);
		return true;
	}

	public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
		return false;
	}

	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		return false;
	}

	public boolean execute(String sql, String[] columnNames) throws SQLException {
		return false;
	}

	public int[] executeBatch() throws SQLException {
		return null;
	}

	/**
	 * Select type query
	 */
	public ResultSet executeQuery(String sql) throws SQLException {
		log.debug("select : "+sql);
		//NOTA BENE: qui posso lavorare sull'sql con i "?" da mettere a posto nel caso di query precalcolate o come si dice .. 
		PResultSet res = this.currentDatabase.executeQuery(sql);
		return new PrologResultSet(sql,res,this.currentDatabase, null);
		
	}
	
	public int executeUpdate(String sql) throws SQLException {
		log.debug("update : "+sql);
		PResultSet affectedRow = this.currentDatabase.executeQuery(sql);
		affectedRow.next(); //vado alla prima riga
		return Integer.parseInt(affectedRow.getValue("AffectedRow").toString());
	}

	public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
		
		return 0;
	}

	public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
		
		return 0;
	}

	public int executeUpdate(String sql, String[] columnNames) throws SQLException {
		
		return 0;
	}

	public Connection getConnection() throws SQLException {
		return this.conn;
	}

	public int getFetchDirection() throws SQLException {
		
		return 0;
	}

	public int getFetchSize() throws SQLException {
		
		return 0;
	}

	public ResultSet getGeneratedKeys() throws SQLException {
		
		return null;
	}

	public int getMaxFieldSize() throws SQLException {
		
		return 0;
	}

	public int getMaxRows() throws SQLException {
		
		return 0;
	}

	public boolean getMoreResults() throws SQLException {
		
		return false;
	}

	public boolean getMoreResults(int current) throws SQLException {
		
		return false;
	}

	public int getQueryTimeout() throws SQLException {
		
		return 0;
	}

	/**
	 * Retrieves the current result as a <code>ResultSet</code> object. This method should be called only once per result.
	 */
	public ResultSet getResultSet() throws SQLException {
		ResultSet r  = this.lastResultSet;
		this.lastResultSet = null;
		return r;
	}

	public int getResultSetConcurrency() throws SQLException {
		
		return 0;
	}

	public int getResultSetHoldability() throws SQLException {
		
		return 0;
	}

	public int getResultSetType() throws SQLException {
		
		return 0;
	}

	public int getUpdateCount() throws SQLException {
		
		return 0;
	}

	public SQLWarning getWarnings() throws SQLException {
		
		return null;
	}

	public boolean isClosed() throws SQLException {
		
		return false;
	}

	public boolean isPoolable() throws SQLException {
		
		return false;
	}

	public void setCursorName(String name) throws SQLException {
		
		
	}

	public void setEscapeProcessing(boolean enable) throws SQLException {
		
		
	}

	public void setFetchDirection(int direction) throws SQLException {
		
		
	}

	public void setFetchSize(int rows) throws SQLException {
		
		
	}

	public void setMaxFieldSize(int max) throws SQLException {
		
		
	}

	public void setMaxRows(int max) throws SQLException {
		
		
	}

	public void setPoolable(boolean poolable) throws SQLException {
		
		
	}

	public void setQueryTimeout(int seconds) throws SQLException {
		
		
	}

	public boolean isWrapperFor(Class arg0) throws SQLException {
		
		return false;
	}

	public Object unwrap(Class arg0) throws SQLException {
		
		return null;
	}

}
